﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SMALLERP.DataClass;
using System.Threading;
using System.Timers;
using System.IO;
using SMALLERP.ComClass;

namespace XJLSC.QM
{
    
    public partial class WLMSHKB : Form
    {
        
        Thread myThread;
        public int frequency = 10000;//更新时间频率
        public static bool isUse = false;//是否停止更新
        public static string statusInfo = string.Empty;//状态
        private delegate void myDelegate(DataTable dt);//定义委托
        private readonly DataBase db = new DataBase();
        public WLMSHKB()
        {
            InitializeComponent();
            label3.Text = "更新频率为:" + (frequency / 1000).ToString() + "秒";
        }
        delegate void show();
        Thread t = null;
        protected override void OnPaint(PaintEventArgs e)
        {
            try
            {
                base.OnPaint(e);
            }
            catch
            {

                Invalidate();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
                    
             
        }
       
        private void WLMSHKB_Load(object sender, EventArgs e)
        {
            trackBar1.Value = 300000;
            radioButton1.Checked = true;
            //开始查询数据
            myThread = new Thread(startFillDv);//实例化线程
            myThread.Start();
            t = new Thread(new ThreadStart(ShowLbl));
            t.Start();
               
        }

          public void ShowLbl()
        {
            while (true)
            {
                if (panel1.InvokeRequired)
                {
                    panel1.Invoke(new show(GetConfig));
                }
                Thread.Sleep(200);
            }
        }

        private void GetConfig()
        {
            try
            {  string kanban="";
            kanban = OperatorFile.GetIniFileString("KanBan", "kanban1", "",
                                                  Application.StartupPath + "\\QM.ini");
                int k = System.Text.Encoding.Default.GetByteCount(kanban);
                if (k < 128)
                {
                    for (int i = k; i <= 128; i++)
                    {
                        kanban += " ";
                    }
                }
                this.label2.Text = kanban;
                this.label1.Text = kanban;                                                                                                               
                this.label1.Left = label1.Left - 10;
                this.label2.Left = label1.Left + this.label1.Size.Width;
                if (label1.Left + label1.Size.Width <= 0) 
                {
                    label1.Left = label2.Left;
                    label2.Left = label1.Left + this.label1.Size.Width;
                }
            }
            catch (Exception ex)
            {
            }
        }


        private void startFillDv()
        {
            while (true)
            {
                if (isUse)
                {
                    statusInfo = "正在实时更新数据......";
                    string strSql = null;
                    if (radioButton1.Checked)
                    {
                        strSql = "SELECT V_GXXL.TY009 as 线别,V_GXXL.TY001 as 工单单别,V_GXXL.TY002 as 工单单号,MOCTA.TA034 产品品名,V_GXXL.TB003 材料品号,INVMB1.UDF01 材料老编码,INVMB1.MB002 材料品名,INVMB1.MB003 材料规格,CEILING((CASE WHEN ISNULL(V_GXLL.TE005,0)>0 THEN V_GXXL.XL-ISNULL(V_GXLL.TE005,0) ELSE V_GXXL.XL END)) as 缺领数,CEILING(CASE WHEN ISNULL(V_INVMC_MOCTE.KYKC,0)-ISNULL(V_DL.TE005,0)<0 THEN 0 ELSE ISNULL(V_INVMC_MOCTE.KYKC,0)-ISNULL(V_DL.TE005,0) END) 可用库存,CEILING(V_GXXL.TY004) 排产数,CEILING(V_GXXL.XL) 需领量,CEILING(ISNULL(V_GXLL.TE005,0)) 已领量,INVMB1.MB017 仓库,CEILING(ISNULL(INVMC.MC007,0)) 仓库库存,V_GXXL.TY019 工艺,RTRIM(ISNULL(MW002,'****')) 工艺名称  ";
                        strSql = strSql + "FROM V_GXXL  Left JOIN V_GXLL On V_GXXL.TY001=V_GXLL.TE011 and V_GXXL.TY002=V_GXLL.TE012 and V_GXXL.TB003=V_GXLL.TE004 and V_GXXL.TY019=V_GXLL.TE009 and V_GXXL.TY009=V_GXLL.TC005 Left JOIN INVMB INVMB1 On V_GXXL.TB003=INVMB1.MB001 Left JOIN MOCTA On V_GXXL.TY001=MOCTA.TA001 and V_GXXL.TY002=MOCTA.TA002 LEFT JOIN INVMB INVMB2 ON INVMB2.MB001=TA006 LEFT JOIN V_INVMC_MOCTE ON PH=V_GXXL.TB003 AND CK=INVMB1.MB017 LEFT JOIN INVMC ON INVMC.MC001=V_GXXL.TB003 AND INVMC.MC002=INVMB1.MB017 LEFT JOIN V_DL ON V_GXXL.TB003=V_DL.TE004 AND INVMB1.MB017=V_DL.TE008 LEFT JOIN CMSMW ON V_GXXL.TY019=MW001 WHERE INVMB1.MB042='1' AND ((MOCTA.TA011 <> 'Y') AND (MOCTA.TA011 <> 'y') AND (V_GXXL.TY018 <> '') AND (V_GXXL.XL > 0)) AND V_GXXL.XL-ISNULL(V_GXLL.TE005,0)>0   ORDER BY V_GXXL.TY001 asc,V_GXXL.TY002 asc,V_GXXL.TY009 asc";
                   
                    }


                    try
                    {
                        DataTable dt;
                       
                        //dataGridView1.DataSource = "";
                        dt = db.GetDataSet1(strSql, "PURTD").Tables["PURTD"];
                        Grid(dt);
                        //dataGridView1.DataSource = dt;
                        //dataGridView1.Refresh();
                        //this.dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.LightGray;
                       // this.dataGridView1.AllowUserToAddRows = false;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "软件提示");
                        //throw ex;
                    }
                    Thread.Sleep(frequency);
                }
                else
                {
                    statusInfo = "停止更新!";
                }
            }
        }

        private void Grid(DataTable dt)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new myDelegate(Grid), new object[] { dt });
            }
            else
            {
                try
                {
                    this.dataGridView1.DataSource = null;
                    this.dataGridView1.DataSource = dt;
                    dt = null;
                    statusInfo = "更新完成!";
                }
                catch
                {

                }
            }

        }

        private void WLMSHKB_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (this.myThread.IsAlive)
            {
                this.myThread.Abort();//结束线程
            }
           
        }


        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                isUse = true;
            }
            else
            {
                isUse = false;
            }
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            label3.Text = "更新频率为:" + (trackBar1.Value / 1000).ToString() + "秒";
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            label4.Text = statusInfo;
            frequency = trackBar1.Value;
          
            //if (statusInfo.Trim() == "正在实时更新数据......")
            //{
            //    pictureBox1.Visible = true;
            //}
            //else
            //{
            //    pictureBox1.Visible = false;
            //}
        }

        private void WLMSHKB_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (t != null)
            {
                t.Abort();
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "Execl files (*.xls)|*.xls";
            dlg.FilterIndex = 0;
            dlg.RestoreDirectory = true;
            dlg.CreatePrompt = true;
            dlg.Title = "保存为Excel文件";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                Stream myStream;
                myStream = dlg.OpenFile();
                StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
                string columnTitle = null;
                try
                {
                    //写入列标题    
                    for (int i = 0; i < dataGridView1.ColumnCount; i++)
                    {
                        if (dataGridView1.Columns[i].Visible == true)
                        {
                            if (i > 0)
                            {
                                columnTitle += "\t";
                            }
                            columnTitle += dataGridView1.Columns[i].HeaderText;

                        }

                    }
                    sw.WriteLine(columnTitle);
                    //写入列内容    
                    for (int j = 0; j < dataGridView1.Rows.Count; j++)
                    {

                        string columnValue = null;
                        for (int k = 0; k < dataGridView1.Columns.Count; k++)
                        {
                            if (dataGridView1.Columns[k].Visible == true)
                            {
                                if (k > 0)
                                {
                                    columnValue += "\t";
                                }
                                if (dataGridView1.Rows[j].Cells[k].Value == null)
                                    columnValue += "";
                                else
                                    columnValue += dataGridView1.Rows[j].Cells[k].Value.ToString().Trim();
                            }
                        }
                        sw.WriteLine(columnValue);
                    }
                    sw.Close();
                    myStream.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                finally
                {
                    sw.Close();
                    myStream.Close();
                }
            }


        }




        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (checkBox1.CheckState == CheckState.Checked)
            {
                double kykc = (this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["可用库存"].Index].Value == null) ? 0.00 : Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["可用库存"].Index].Value);
                double qls = (this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["缺领数"].Index].Value == null) ? 0.00 : Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["缺领数"].Index].Value);
                if (kykc >= qls)
                {

                    for (int i = 0; i <= this.dataGridView1.Columns.Count - 1; i++)
                    {
                        this.dataGridView1.Rows[e.RowIndex].Cells[i].Style.BackColor = Color.Green;
                    }
                }
            }
        }


 
    }
}
